package com.xiaoyao.server.service.impl;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoyao.server.entity.User;
import com.xiaoyao.server.service.UserService;
import com.xioayao.server.exception.BizException;
import com.xiaoyao.server.dao.UserDao;
import com.xiaoyao.server.dto.RegistExecution;

import org.slf4j.Logger;

@Service
public class UserServiceImpl implements UserService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserDao userDao;
	
	public User getById(long id) {
		return userDao.queryById(id);
	}
	
	public List<User> getUserList() {
		return userDao.queryAll(0, 100);
	}
	
	@Transactional
	public RegistExecution regist(String username, String password) {
		try {
			// 注册
			User user = new User();
			int addflag;
			
			user.setName(username);
			user.setPassword(password);
			addflag = userDao.addUser(user);
			if (addflag != 1) {// 账号重复	
				return new RegistExecution(false,"账号重复！");
			} else {
				return new RegistExecution(true,"成功！");
			}
		// 要先于catch Exception异常前先catch住再抛出，不然自定义的异常也会被转换为AppointException，导致控制层无法具体识别是哪个异常
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// 所有编译期异常转换为运行期异常
			throw new BizException("register error:" + e.getMessage());
		}
	}

	@Override
	public User loginu(String username, String password) {//用户名登录
		try {
			// 注册
			User user = userDao.loginu(username,password);
			return user;
		// 要先于catch Exception异常前先catch住再抛出，不然自定义的异常也会被转换为AppointException，导致控制层无法具体识别是哪个异常
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// 所有编译期异常转换为运行期异常
			throw new BizException("register error:" + e.getMessage());
		}
	}
	
	@Override
	public User loginp(String phone, String password) {//手机登录
		try {
			// 注册
			User user = userDao.loginp(phone,password);
			return user;
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			// 所有编译期异常转换为运行期异常
			throw new BizException("register error:" + e.getMessage());
		}
	}
    /**
     * 个人界面 修改用户名、手机号、头像
     * @return 
     */
	@Override
	public int alterAvatarUrl( long id , String avatarUrl){
		try {
			int flag = userDao.alterAvatarUrl(id,avatarUrl);
			return flag;
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			//return 0;
			throw new BizException("register error:" + e.getMessage());
		}
	}
	@Override
	public int alterPhone( long id , String phone){
		try {
			int flag = userDao.alterPhone(id,phone);
			return flag;
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			//return 0;
			throw new BizException("register error:" + e.getMessage());
		}
	}
	@Override
	public int alterName( long id ,String username){
		try {
			int flag = userDao.alterName(id,username);
			return flag;
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			//return 0;
			throw new BizException("register error:" + e.getMessage());
		}
	}
	
	@Override
	public String getHeadPhoto( long id ){
		try {
			return userDao.getHeadPhoto(id);
		} catch (BizException e1) {
			throw e1;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			//return 0;
			throw new BizException("register error:" + e.getMessage());
		}
	}

	
}
